package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.ContractActivity;
import com.gongpingjialoan.bean.AssessHistory;

import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class ContractAdapter extends BaseAdapter {

    private List<AssessHistory> mAssessHistories;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public ContractAdapter(List<AssessHistory> assessHistories, Context context) {
        mAssessHistories = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<AssessHistory> mAssessHistories) {
        this.mAssessHistories = mAssessHistories;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mAssessHistories.size();
    }

    @Override
    public Object getItem(int position) {
        return mAssessHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.contract_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.price);
            holder.domainTextView = (TextView) convertView.findViewById(R.id.domain);
            holder.reviewTextView = (TextView) convertView.findViewById(R.id.review);

            holder.contract_typeTextView = (TextView) convertView.findViewById(R.id.contract_type);
            holder.settingTextView = (TextView) convertView.findViewById(R.id.setting);
            holder.settingImageView = (ImageView) convertView.findViewById(R.id.image);
            holder.settingView = convertView.findViewById(R.id.setting_layout);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        AssessHistory source = mAssessHistories.get(position);
        convertView.setTag(R.id.tag_second, source);

        if ("false".equals(source.review)) {
            holder.reviewTextView.setVisibility(View.GONE);
        } else {
            holder.reviewTextView.setText("已下架");
            holder.reviewTextView.setVisibility(View.VISIBLE);
        }

        if(TextUtils.isEmpty(source.city)){
            holder.domainTextView.setVisibility(View.GONE);
        }else{
            holder.domainTextView.setVisibility(View.VISIBLE);
            holder.domainTextView.setText(source.city + "车源");
        }

        holder.tvCarTitle.setText(source.title);
        holder.mileTextView.setText(source.year + "年上牌/" + source.mile + "万公里");
        holder.timeTextView.setText(source.create_time);
        holder.priceTextView.setText(source.price);

        if("phone".equals(source.type)){
            holder.contract_typeTextView.setVisibility(View.VISIBLE);
            holder.contract_typeTextView.setText("已电话联系");
        }else if("message".equals(source.type)){
            holder.contract_typeTextView.setVisibility(View.VISIBLE);
            holder.contract_typeTextView.setText("已短信联系");
        }else{
            holder.contract_typeTextView.setVisibility(View.GONE);
        }


        if(!TextUtils.isEmpty(source.contact_result)){
            holder.settingTextView.setText(source.contact_result);
            holder.settingImageView.setVisibility(View.GONE);
            holder.settingView.setOnClickListener(null);

        } else {
            holder.settingTextView.setText("设置结果");
            holder.settingImageView.setVisibility(View.VISIBLE);
            holder.settingView.setOnClickListener(mOnClickListener);
            holder.settingView.setTag(position);
        }

        Glide.with(mContext).load(source.url).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);
        return convertView;
    }


    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ContractActivity contractActivity = (ContractActivity) mContext;
            contractActivity.showPopup(v);
        }
    };

    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarTitle;

        public TextView timeTextView;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView domainTextView;

        public TextView reviewTextView;

        public TextView contract_typeTextView;

        public TextView settingTextView;

        public ImageView settingImageView;

        public View settingView;

    }
}

